﻿using System;
using System.Linq;
using NUnit.Framework;
using ProjetoComperio.BusinessLogicLayer;
using ProjetoComperio.DataLayer.Entities;
using System.Web.Mvc;
using System.Web.SessionState;
using ProjetoComperio.Presentation.Web.Controllers;
using ProjetoComperio.Test.FakeData;
using ProjetoComperio.Test.WebUI.FakeObjects;

namespace ProjetoComperio.Test.WebUI.Controllers
{
    [TestFixture]
    public class ProjectControllerTest
    {
        ProjectController projectController;
        FormCollection formCollection;
        ProjectBLL projectBLL;

        //[Test]
        //public void TestValidateForm()
        //{
        //    formCollection["Title"] = "Test";
        //    formCollection["Description"] = "Description Test";
        //    formCollection["SourceInfo"] = "SourceInfo Test";
        //    formCollection["RegisterDate"] = "10/12/2009";
        //    projectController.ValidateForm(formCollection);
        //}

        [TestFixtureSetUp]
        public void TestFixtureSetUp()
        {
            projectBLL = new ProjectBLL();
            projectController = new ProjectController(projectBLL);
        }

        [SetUp]
        public void SetUp()
        {
            projectBLL.InitializeDataContext(new FakeComperioEntitiesDataModel());
            SessionStateItemCollection sessionStateItemCollection = new SessionStateItemCollection();
            projectController.ControllerContext = new FakeControllerContext(projectController, sessionStateItemCollection, CommonData.UserName);
            formCollection = new FormCollection();
        }

        [Test]
        public void TestIndex()
        {
            var actionResult = projectController.Index();
            Assert.IsNotNull((projectController.ViewData["GridSource"] as IQueryable<Project>));
        }

        [Test]
        public void Create()
        {
            var project = FillValidProject();
            projectController.Create(project);
            Assert.IsTrue(projectBLL.RetrieveProjects().Any(x => x.Title.Equals(project.Title)));

        }

        private Project FillValidProject()
        {
            var project = new Project();

            project.Title = "Test" + DateTime.Now;
            project.Description = "Description Test";
            project.SourceInfo = "SourceInfo Test";
            project.RegisterDate = DateTime.Now;

            return project;
        }

        //[Ignore]
        //public void TestValidateFormInvalidFormCollection()
        //{
        //    formCollection["Title"] = String.Empty;
        //    formCollection["SourceInfo"] = String.Empty;
        //    formCollection["Description"] = "Description Test";
        //    formCollection["RegisterDate"] = "10/12/2009";

        //    var result = projectController.ValidateForm(formCollection);
        //}
        //public FormCollection GenerateValidFormCollection()
        //{

        //    var _formCollection = new FormCollection();
        //    var obj = new ProjectBLLFacade().RetrieveProjects().FirstOrDefault();


        //    formCollection.Add("Title", obj.Title);
        //    formCollection.Add("RegisterDate", obj.RegisterDate.ToString());
        //    formCollection.Add("SourceInfo", obj.SourceInfo);
        //    formCollection.Add("Description", obj.Description);
        //    return _formCollection;
        //}

        //[Test]
        //public void TestEditValidEntity()
        //{
        //    Assert.IsNotNull(projectController.Edit(GenerateValidFormCollection()));

        //}

    }
}
